<?php

namespace Admin\Model;

use Think\Model;

/**
*会员模型
*/
class AlbumModel extends Model
{
	/**
	 * [showAll description]查找UID所有的相册
	 * @param  [type] $id [description]要查相册的UID
	 * @return [type]     [description]
	 */
	public function showAll($id){
		return $this ->where(array('uid' =>$id,'status' =>1)) ->order(array('create_time' =>'desc')) ->select();
	}
	/**
	 * [delNull description]删除图片为空的相册
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function delNull($data){
	foreach ($data as $key => &$value) {
				if($value['img'] == ''){
					$id = $value['id'];
					$this ->where(array('id' =>$id)) ->save(array('status' =>0));
				}
			}
		return $data;
	}
	/**
	 * [selectImg description]循环替换相册
	 * @param  [type] $data [description]上面取出来的所有数据
	 * @return [type]       [description]
	 */
	public function selectImg($data){
		foreach ($data as &$value) {
			$value['img'] = $this ->attrImg($value['img']);
		}
		return $data;
	}
	/**
	 * [attrImg description]把字符串该为数组
	 * @param  [type] $img [description]图片的字符串
	 * @return [type]      [description]
	 */
	public function attrImg($img){
		$data = explode(",",$img);
		foreach ($data as $number) {
		$numbers[] = intval(trim($number)); //因为可能数字两侧有空格，所以trim一下
	    }
	    return $numbers;
	}
	/**
	 * [ajaxDel description]ajax删除
	 * @param  [type] $post [description]
	 * @return [type]       [description]
	 */
	public function ajaxDel($post){
		$img  = $post['img'];
		$id   = $post['id'];
		$img_str = $this->where( array('id'=>$id) )->getField('img');
		$img_replace = str_replace($img,'',$img_str);
		$img_arr = explode(',',$img_replace);
		foreach($img_arr as $vo){
			if(!empty($vo)){
				$new_arr[] = $vo;
			}
		}
		$str_img = implode(',',$new_arr);
		$data = $this ->where(array('id' =>$id)) ->save(array('img' =>$str_img));
		return $data;
	}
}